package com.examination.to.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.examination.to.entity.QuestionBank;

import com.examination.to.entity.QuestionToBank;
import com.examination.to.mapper.QuestionBankMapper;


import com.examination.to.mapper.QuestionToBankMapper;
import com.examination.to.service.QuestionBankService;


import com.examination.to.util.GeneralConstant;

import com.examination.to.util.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements QuestionBankService, GeneralConstant {


    @Autowired
    private QuestionBankMapper questionBankMapper;

    @Autowired
    private QuestionToBankMapper questionToBankMapper;


    public Result addQuestionBank(QuestionBank questionbank){
        Result result = new Result();
        result.setCode(-1);
        QuestionBank qubank = new QuestionBank()
                .setBankname(questionbank.getBankname());
        try {
            if (questionbank != null){
                questionBankMapper.insert(qubank);
            }else {
                throw new NullPointerException("值为空");
            }
            result.setCode(200);
            result.setMsg("题库创建成功");
            result.setDetail(qubank);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public Result deleteQuestionBank(Integer bankid){
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        if (bankid == null || queryWrapper.eq("bankid",bankid).equals("")){
            return null;
        }
        questionBankMapper.delete(queryWrapper.eq("bankid",bankid));
        QueryWrapper<QuestionToBank> questionToBankQueryWrapper = new QueryWrapper<>();
        questionToBankMapper.deleteById(questionToBankQueryWrapper.eq("bankid",bankid));
        return new Result(200,"删除成功");
    }

    public Result updateQuestionBank(QuestionBank questionBank){
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        if (queryWrapper.eq("bankid",questionBank.getId()).equals("") || questionBank.equals("")){
            return null;
        }
        questionBankMapper.updateById(questionBank);
        return new Result(200,"修改成功");
    }


}
